package com.hp.bon.sgw.wh.bean;

import java.util.List;

import org.apache.commons.lang.StringUtils;



/**
 * 发出外网的黑卡响应消息
 * @author Junkai
 *
 */
public class BlackCardMsgOUt extends BlackCardMsg {

	//Result	1	结果码	Unsigned32	N	区号，例如武汉是027
	private String result;
	
	//ServId	1	用户标示	UTF8String	N	
	private String servId;
	
	private List<DataInfomation> dataInfomations;
	
	//错误消息
	private String retMsg;
		
	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getServId() {
		return servId;
	}

	public void setServId(String servId) {
		this.servId = servId;
	}
	
	public List<DataInfomation> getDataInfomations() {
		return dataInfomations;
	}

	public void setDataInfomations(List<DataInfomation> dataInfomations) {
		this.dataInfomations = dataInfomations;
	}
	
	public String getRetMsg() {
		return retMsg;
	}

	public void setRetMsg(String retMsg) {
		this.retMsg = retMsg;
	}

	public String toXml() {
		processNull();
		StringBuilder msg = new StringBuilder();
		//失败消息
		if (!result.equals("0")) {
			msg.append("<Service_Information>");
			msg.append("<Result>%s</Result>");
			msg.append("<Acc_Nbr>%s</Acc_Nbr>");
			msg.append("<ServId>%s</ServId>");
			msg.append("<Ret_Msg>%s</Ret_Msg>");
			msg.append("</Service_Information>");
			return String.format(msg.toString(), 
					this.getResult(),this.getAccNbr(),
					this.getServId(),this.getRetMsg());
		}
		else {
			
			//成功消息
			msg.append("<Service_Information>");
			msg.append("<Result>%s</Result>");
			msg.append("<Acc_Nbr>%s</Acc_Nbr>");
			msg.append("<ServId>%s</ServId>");
			
			if (null!= this.dataInfomations) {
				for(DataInfomation info:this.dataInfomations){
					StringBuilder dataSb = new StringBuilder();
					dataSb.append("<Data_Information>");
					dataSb.append("<CkMDN>%s</CkMDN>");
					dataSb.append("<CkCount>%s</CkCount>");
					dataSb.append("</Data_Information>");
					info.processNull();
					msg.append(String.format(dataSb.toString(), info.getCkmdn(),info.getCkcount()));
				}
			}
			
			msg.append("</Service_Information>");
			return String.format(msg.toString(), 
					this.getResult(),this.getAccNbr(),
					this.getServId(),this.getRetMsg());
		}
	}

	public void processNull() {
		if (StringUtils.isEmpty(this.getResult())) {
			this.setResult("");
		}
		if (StringUtils.isEmpty(this.getAccNbr())) {
			this.setAccNbr("");
		}
		if (StringUtils.isEmpty(this.getServId())) {
			this.setServId("");
		}
		if (StringUtils.isEmpty(this.getRetMsg())) {
			this.setRetMsg("");
		}
	}

	public static void main(String[] args) {
		BlackCardMsgOUt err = new BlackCardMsgOUt();
		System.out.println(err.toXml());
	}
	

}
